package net.jiastudy.core.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringUtility implements ApplicationContextAware {

	@Autowired
	private static ApplicationContext applicationContext;

	@Override
	public void setApplicationContext(final ApplicationContext applicationContext) {
		SpringUtility.applicationContext = applicationContext;
	}

	/*
	 * Get a class bean from the application context
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getBean(final Class<?> clazz) {
		return (T) applicationContext.getBean(clazz);
	}

	@SuppressWarnings("unchecked")
	public static <T> T getBean(String name) {
		return (T) applicationContext.getBean(name);
	}

	/*
	 * Return the application context if necessary for anything else
	 */
	public static ApplicationContext getContext() {
		return applicationContext;
	}

}
